package cn.newgrand.netcall.ui.questionRecord;

import java.awt.Component;
import java.awt.Insets;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

import org.jivesoftware.spark.util.log.Log;

public class AttachmentContainer extends JTextPane{
	
	/**
     * The SimpleAttributeSet used within this instance of JTextPane.
     */
    public final SimpleAttributeSet styles = new SimpleAttributeSet();

	private static final long serialVersionUID = 1L;
	private Map<String, FilePosition> attachFiles = new HashMap<String, FilePosition>();
	private int componentLength = 0;
	
	public AttachmentContainer(){
		this.setEditable(false);
		setMargin(new Insets(10, 10, 0, 10));
	}

	/**
     * Set the current font size.
     *
     * @param size the current font size.
     */
    public void setFontSize(int size) {
        StyleConstants.setFontSize(styles, size);
        try {
            setCharacterAttributes(styles, false);
        }
        catch (Exception fontException) {
            Log.error("Error settings font:", fontException);
        }
    }
    
    /**
     * 增加一个文件到附件容器
     *
     * @param component the component to insert.
     */
    public void addComponent(Component component,File file) {
    	String filename = file.getName();
        final StyledDocument doc = (StyledDocument)getDocument();
        // The image must first be wrapped in a style
        Style style = doc.addStyle(filename, null);


        StyleConstants.setComponent(style, component);

        // Insert the image at the end of the text
        try {
            doc.insertString(doc.getLength(), "ignored text", style);
            doc.insertString(doc.getLength(), "\n", null);
        }
        catch (BadLocationException e) {
            Log.error(e);
        }
        if(componentLength == 0){
        	componentLength = doc.getLength();
        }
        FilePosition filePosition = new FilePosition(file, attachFiles.size());
        attachFiles.put(filename, filePosition);
    }
    
    public void removeComponent(String style){
    	int filePosition = 0;
    	if(attachFiles.containsKey(style)){
    		FilePosition position = attachFiles.get(style);
    		filePosition = position.getPosition();
    		attachFiles.remove(style);
    		//将位置大于fileposition都减去1
    		for(FilePosition fp : attachFiles.values()){
    			if(fp.getPosition() > filePosition){
    				fp.setPosition(fp.getPosition() -1 );
    			}
    		}
    	}
    	final StyledDocument doc = (StyledDocument)getDocument();
    	try {
			doc.remove(filePosition*componentLength, componentLength);
		} catch (BadLocationException e) {
			e.printStackTrace();
		}
    }
    
    protected class FilePosition{
    	File file;
    	int position;
    	
    	public FilePosition(File file,int position) {
			this.file = file;
			this.position = position;
		}

		public File getFile() {
			return file;
		}

		public void setFile(File file) {
			this.file = file;
		}

		public int getPosition() {
			return position;
		}

		public void setPosition(int position) {
			this.position = position;
		}
    }

	public Map<String, FilePosition> getAttachFiles() {
		return attachFiles;
	}

	public void setAttachFiles(Map<String, FilePosition> attachFiles) {
		this.attachFiles = attachFiles;
	}
    
}
